package PimpDBManager;

import java.io.*;
import java.net.*;
import netcom.*;

class Client
{
    Socket server;
    String server_addr;
    int server_port;
    Protocol protocol;
    private static final String SENDING_FILE = "SENDING_FILE";
    private static final String LOGIN = "LOGIN";
    private static final String OK = "OK";
    private static final String WRONG = "WRONG";
    private static final String GET_FILE = "GET_FILE";
    String user;
    String pass;

    Client(String _user, String _pass)
    {
        server_addr = "localhost";
        server_port = 5000;
        server = null;
        protocol = new Protocol();
        user = _user;
        pass = _pass;
        connect();
    }
    Client(String _user, String _pass, String server_addr, int port)
    {
        this.server_addr = server_addr;
        server_port = port;
        server = null;
        protocol = new Protocol();
        user = _user;
        pass = _pass;
        connect();
    }
    public String getServer()
    {
        return server_addr;
    }
    public int getPort()
    {
        return server_port;
    }
    public void changeServer(String server_addr, int server_port)
    {
        clean();
        this.server_addr = server_addr;
        this.server_port = server_port;
        connect();
    }

    public void connect()
    {
        System.out.println("Connecting to server");
        try
        {
            server = new Socket(server_addr, server_port);
        }
        catch (UnknownHostException e)
        {
            System.out.println("Unknown host: " + server_addr);
            System.exit(1);
        }
        catch(IOException e)
        {
            System.out.println("Could not connect to the server");
            System.exit(1);
        }
        protocol.negotiateWithServer(server);
        //protocol.sendMsg("start dluga wiadomosc bardzo kurna dluga.... taka ze hoho, nie wiadomo ile jeszcze do konca moze byc :D.... ble ble ble hahahahah to jeszcze nie koniec wiadomosci.... a teraz juz tak... koniexcstart dluga wiadomosc bardzo kurna dluga.... taka ze hoho, nie wiadomo ile jeszcze do konca moze byc :D.... ble ble ble hahahahah to jeszcze nie koniec wiadomosci.... a teraz juz tak... koniexc abcabcabcabca  bcabcabcabcabcabc");
        //protocol.sendMsg("ty tepy chuju !!!\nble ble ble ble !!\n ty tepy chuju !!!");
        int logins = 3;
        String answer = login();
        if(answer.compareTo(OK) == 0)
        {
            System.out.println("Logged in");
        }
        if(answer.compareTo(WRONG) == 0)
        {
            while(logins-- > 0)
            {
                // set new user, pass
                // try to login
                login();
            }
        }
    }

    private String login()
    {
        System.out.println("Logging in...");
        protocol.sendMsg(LOGIN);
        protocol.sendMsg(user);
        protocol.sendMsg(pass);
        return protocol.getMsg();
    }

    public void saveData(String data)
    {
        System.out.println("Sending data");
        protocol.sendMsg(SENDING_FILE);
        protocol.sendMsg(data);
    }

    public String getData()
    {
        System.out.println("Getting data");
        protocol.sendMsg(GET_FILE);
        return protocol.getMsg();
    }
    public void close()
    {
        protocol.close();
    }
    
    protected void clean()
    {
        try
        {
            server.close();
        } 
        catch(IOException e)
        {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }
    public static void main(String args[])
    {
        // IP wilka: 156.17.225.37
        String user = "user";
        String pass = "pass";
        Client c = null;
        if(args.length == 0)
            c = new Client(user,pass,"localhost",5000);
        if(args.length == 1)
            c = new Client(user,pass,args[0],5000);
        if(args.length == 2)
            c = new Client(user,pass,args[0],new Integer(args[1]));
        // if too many arguments try to take the first two
        if(args.length > 2)
            c = new Client(user,pass,args[0],new Integer(args[1]));
        c.saveData("<moj>\n\t<zajebisty>\n\t\tpikzorrrrsss\n\t</zajebisty>\n<moj>");
        c.close();
    }
}

